package com.gage.controller.app;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gage.controller.base.BaseController;
import com.gage.commons.result.PageInfo;
import com.gage.commons.utils.StringUtils;
import com.gage.model.BsDepartment;
import com.gage.model.BsLeaguer;
import com.gage.service.IBsLeaguerService;

/**
 * <p>
 * 会员管理控制器
 * </p>
 * @author kairu.lu
 * @since 2017-04-26
 */
@Controller
@RequestMapping("/app/leaguer")
public class AppLeaguerController extends BaseController{
	@Autowired
	private IBsLeaguerService leaguerService;
	@GetMapping("/index")
    public String create() {
        return "app/leaguer/index";
    }
	/**
	 * 会员列表
     * @author lkr
     * @date:20170512
	 * @param leaguer
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @return
	 */
    @PostMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(BsLeaguer leaguer, Integer page, Integer rows, String sort, String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String, Object> condition = new HashMap<String, Object>();
        String associationId=this.getShiroUser().getAssociationId().toString();
        if (StringUtils.isNotEmpty(associationId)) {
            condition.put("associationId", associationId);
        }else {
        	 return pageInfo;
		}
        if (StringUtils.isNotEmpty(leaguer.getTrueName())) {
            condition.put("trueName", leaguer.getTrueName());
        }
        pageInfo.setCondition(condition);
        leaguerService.selectDataGrid(pageInfo);
        return pageInfo;
    }
    /**
     * 添加会员信息页
      * @author lkr
      * @date:20170512
     * @return
     */
    @GetMapping("/add")
    public String addPage(String departmentId,Model model) {
        return "app/leaguer/edit";
    }
    /**
     * 编辑会员页
     *
     * @return
     */
    @GetMapping("/edit")
    public String editPage(String id,Model model) {
    	if (StringUtils.isNotBlank(id)) {
			BsLeaguer leaguer=leaguerService.selectById(Integer.parseInt(id));
			model.addAttribute("leaguer",leaguer);
		}
        return "app/leaguer/edit";
    }
    /**
     * @author lkr
     * @date:20170512
     * @param leaguer
     * @param result
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
	public Object save(@Valid BsLeaguer leaguer, BindingResult result){
    	if (result.hasErrors()) {
            return renderError(result);
        }
    	leaguer.setAssociationId(this.getShiroUser().getAssociationId());
    	if (StringUtils.isEmpty(leaguer.getAssociationId())) {
    		return resultJson("0","网络故障","");
		}
    	if (StringUtils.isNotEmpty(leaguer.getId())) {
    		leaguerService.updateById(leaguer);
		}else {
			leaguer.setInsertTime(new Date());
			leaguerService.insert(leaguer);
		}
    	return resultJson("1","信息保存成功","");
    }
    /**
     * 删除
     * @author lkr
 	 * @date:20170512
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
    	if (StringUtils.isNotEmpty(id)) {
    		leaguerService.deleteById(Integer.parseInt(id));
    		return renderError("操作成功！");
		}else {
			return renderError("删除失败，网络出错！");
		}
	}
}
